package com.wskeee.ec.components
{
	import com.wskeee.ec.utils.ScrollPolicy;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;

	public class TextAera extends TextInput
	{
		public function TextAera(text:String="")
		{
			super(text);
			setActualSize(100,100);
		}
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var calculateTF:TextField;
		private var fixScrollBarFlag:Boolean = false;
		private var lineHeight:Number = 15;
		private var textScrollChangeFlag:Boolean = false;
		private var vScrollChangeFlag:Boolean = false;
		/**
		 * 控制 textField 垂直滚动 
		 */
		protected var verticalScrollBar:VScrollBar;
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  verticalScrollBar
		//-----------------------------------------------------------------
		/**
		 * 垂直滚动策略,on总是显示/auto自动/off关闭显示 
		 */
		private var _verticalScrollPolicy:String = "auto";
		/**
		 * 设置垂直滚动策略 
		 * @param value	ScrollPolicy.ON 总是显示/ScrollPolicy.AUTO 自动/ScrollPolicy.OFF 关闭显示
		 * 
		 */
		public function set verticalScrollPolicy(value:String):void
		{
			_verticalScrollPolicy = value;
			invalidateProperties();
		}
		
		public function get verticalScrollPolicy():String
		{
			return _verticalScrollPolicy;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  init
		//-----------------------------------------------------------------
		override protected function initChildren():void
		{
			super.initChildren();
			calculateTF = new TextField();
			calculateTF.multiline = calculateTF.wordWrap = true;
			_tf.multiline = _tf.wordWrap = true;
			verticalScrollBar = new VScrollBar();
			addTFEvent();
			addVerticalScrollBarEvent();
		}
		
		private function addTFEvent(remove:Boolean=false):void
		{
			if(!remove)
				_tf.addEventListener(Event.SCROLL,onTFScrollHandler);
			else
				_tf.removeEventListener(Event.SCROLL,onTFScrollHandler);
		}
		
		private function addVerticalScrollBarEvent(remove:Boolean=false):void
		{
			if(!remove)
				verticalScrollBar.addEventListener(Event.CHANGE,onVScrollChangeHandler);
			else
				verticalScrollBar.removeEventListener(Event.CHANGE,onVScrollChangeHandler);
		}
		
		private function onVScrollChangeHandler(event:Event):void
		{
			vScrollChangeFlag = true;
			invalidateProperties();
		}
		
		private function onTFScrollHandler(event:Event):void
		{
			textScrollChangeFlag = true;
			invalidateProperties();
		}
		
		//-----------------------------------------------------------------
		//  initEvent
		//-----------------------------------------------------------------
		override protected function initEvent():void
		{
			super.initEvent();
			addEventListener(Event.RESIZE,onResizeHandler);
		}
		
		private function onResizeHandler(event:Event):void
		{
			fixScrollBarFlag = true;
			invalidateProperties();
			invalidateDisplayList();
		}
		//-----------------------------------------------------------------
		//  override 
		//-----------------------------------------------------------------
		//---------------------
		//commitProperties
		//---------------------
		override protected function commitProperties():void
		{
			super.commitProperties();
			var needScrollBar:Boolean = verticalScrollPolicy==ScrollPolicy.ON;
			if(verticalScrollPolicy==ScrollPolicy.AUTO)
			{
				calculateTF.text = _text;
				needScrollBar = calculateTF.maxScrollV > 1;
			}
			
			
			if(verticalScrollPolicy == ScrollPolicy.ON || 
				(verticalScrollPolicy == ScrollPolicy.AUTO && needScrollBar))
			{
				addChild(verticalScrollBar);
				//禁用 crollbar 更新侦听
				addVerticalScrollBarEvent(true);
				verticalScrollBar.maxScrollPosition = _tf.textHeight;
				//设置 scrollbar 每次滚动的值 = 最大滚动值/可滚动行数
				verticalScrollBar.snapInterval = verticalScrollBar.maxScrollPosition/(_tf.maxScrollV-1);
				addVerticalScrollBarEvent(false);
				fixScrollBarFlag = true;
				invalidateDisplayList();
			}else if(verticalScrollPolicy == ScrollPolicy.OFF || (verticalScrollPolicy == ScrollPolicy.AUTO && !needScrollBar))
			{
				if(verticalScrollBar.parent)
					removeChild(verticalScrollBar)
				invalidateDisplayList();
			}
			
			
			if(vScrollChangeFlag)
			{
				//因为vScrollbar的value发生改变，需要更新 text scrollV 值
				vScrollChangeFlag = false;
				addTFEvent(true);
				//计算 scroll %比 x 最大滚动值  = scrollV
				_tf.scrollV = Math.round((verticalScrollBar.scrollPosition/verticalScrollBar.maxScrollPosition)*(_tf.maxScrollV-1))+1;
				addTFEvent(false);
			}
			
			if(textScrollChangeFlag)
			{
				//因为 text 的行数发生改变，需要更新 vscrollbar 的scrollPosition
				textScrollChangeFlag = false;
				addVerticalScrollBarEvent(true);
				//计算 scroll value = 当前text ScrollV/maxScrollV * 最大滚动值 
				verticalScrollBar.scrollPosition = ((_tf.scrollV-1)/(_tf.maxScrollV-1))*verticalScrollBar.maxScrollPosition;
				addVerticalScrollBarEvent(false);
			}
		}
		
		override protected function measure():void
		{
			lineHeight = _tf.getLineMetrics(0).height;
		}
		
		//---------------------
		//updateDisplayList
		//---------------------
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			calculateTF.width = width - 4;
			calculateTF.height = height - 4;
			if(fixScrollBarFlag && verticalScrollBar.parent)
			{
				//设置 滚动条 显示区,用于计算 thumb 的大小。显示区即为 text的可示区域
				verticalScrollBar.viewarea = new Rectangle(0,0,1,_tf.height-4);
				verticalScrollBar.height = height;
				verticalScrollBar.x = width - verticalScrollBar.width;
			}
			if(fixScrollBarFlag)fixScrollBarFlag=false;
			_tf.width = width - (verticalScrollBar.parent ? verticalScrollBar.width+4 : 4);
		}
		
		//--------------------------------------------------------------------------
		//
		//  public Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  destroy
		//-----------------------------------------------------------------
		override public function destory():void
		{
			addTFEvent(true);
			addVerticalScrollBarEvent(true);
			removeEventListener(Event.RESIZE,onResizeHandler);
			super.destory();
		}
	}
}